#include <iostream>
#include <jsoncpp/json/json.h>

#include "compile_run.hpp"
#include "../comm/httplib.h"

using namespace ns_compile_and_run;
using namespace httplib;

void Uasge(const std::string &proc, const std::string &port)
{
    std::cout << "Usage: " << std::endl
              << std::endl;
    std::cout << proc << " " << port << std::endl
              << std::endl;
}

int main(int argc, char *argv[])
{
    //Server svr;
    // 用来进行测试
    // svr.Get("/hello", [](const Request &req, Response &resp)
    // {
    //     resp.set_content("hello httplib,你好 httplib!","text/plain;charset=utf-8");
    // });

    if(argc != 2)
    {
        Uasge(argv[0], argv[1]);
    }

    Server svr;
    svr.Post("/compile_and_run", [](const Request &req, Response &resp)
    {
        //用户的请求的body是一个json串: in_json
        //我们响应也返回一个json串: out_json
        std::string in_json = req.body, out_json("");
        //编译运行功能
        if(!in_json.empty())
        {
            CompileAndRun::Start(in_json, &out_json);
            //得到返回的out_json
            resp.set_content(out_json, "application/json;charset=utf-8");
        }
    });
    svr.Post("/checkonline", [](const Request &req, Response &resp){
        // do noting
    });

    svr.listen("0.0.0.0", atoi(argv[1]));

    return 0;
}